import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;

public class MyFrame extends JFrame {
  private JFrame frame = this;
  private JLabel message = new JLabel("请开局");
  private JButton start = new JButton("开局");
  private JButton again = new JButton("要牌");
  private JButton end = new JButton("不要");
  private JButton exit = new JButton("退出");

  private ArrayList<Card> allCard = new ArrayList<Card>();
  private ArrayList<Card> computerCardList = new ArrayList<Card>();
  private ArrayList<Card> playerCardList = new ArrayList<Card>();

  public void createAllCard() {
    String[] huases = {"黑", "红", "梅", "方"};
    for (int i = 1; i <= 13; i++) {
      for (int k = 0; k < 4; k++) {
        Card c = new Card();
        c.sethuase(huases[k]);
        c.setshuzi(i);
        allCard.add(c);
      }
    }
  }

  public void sendCardToComputer() {
    Random random = new Random();
    int index = random.nextInt(allCard.size());
    Card c = allCard.get(index);
    computerCardList.add(c);
    allCard.remove(index);
  }

  public void sendCardToPlayer() {
    Random random = new Random();
    int index = random.nextInt(allCard.size());
    Card c = allCard.get(index);
    playerCardList.add(c);
    allCard.remove(index);
  }

  public int sumDianShu(ArrayList<Card> list) {
    int sum = 0;
    for (int i = 0; i < list.size(); i++) {
      Card c = list.get(i);
      sum += c.getdianshu();
    }
    return sum;
  }

  public void showCard(ArrayList<Card> list, int y, boolean showFirstCard) {
    int x = 100;
    for (int i = 0; i < list.size(); i++) {
      Card c = list.get(i);
      c.setSize(71, 96);
      if (i == 0 && !showFirstCard) {
        c.loadImg(false);
      } else {
        c.loadImg(true);
      }
      c.setLocation(x, y);
      x += 81;
      this.add(c);
    }
    this.repaint();
  }

  public MyFrame() {
    this.setSize(800, 600);
    this.setLocation(500, 200);
    this.setLayout(null);

    setSize(800, 600);
    setLocationRelativeTo(null);
    String path = "img/背景aa.jpg";
    ImageIcon background = new ImageIcon(path);
    JLabel label = new JLabel(background);
    label.setBounds(0, 0, this.getWidth(), this.getHeight());
    JPanel imagepPanel = (JPanel)this.getContentPane();
    imagepPanel.setOpaque(false);
    this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setTitle("21点纸牌游戏");

    message.setSize(300, 25);
    message.setLocation(100, 400);
    this.add(message);

    start.setSize(100, 25);
    start.setLocation(100, 450);
    start.setEnabled(true);
    this.setVisible(true);
    this.add(start);

    again.setSize(100, 25);
    again.setLocation(250, 450);
    again.setEnabled(false);
    this.add(again);

    end.setSize(100, 25);
    end.setLocation(400, 450);
    end.setEnabled(true);
    this.add(end);

    exit.setSize(100, 25);
    exit.setLocation(550, 450);
    exit.setEnabled(true);
    this.add(exit);

    start.addActionListener(new start());
    again.addActionListener(new again());
    end.addActionListener(new end());
    exit.addActionListener(new exit());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  class start implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      for (int i = 0; i < playerCardList.size(); i++) {
        Card c = playerCardList.get(i);
        frame.remove(c);
      }
      for (int i = 0; i < computerCardList.size(); i++) {
        Card c = computerCardList.get(i);
        frame.remove(c);
      }

      message.setText("游戏中");
      allCard.clear();
      playerCardList.clear();
      computerCardList.clear();

      createAllCard();
      sendCardToComputer();
      sendCardToPlayer();
      showCard(computerCardList, 80, false);
      showCard(playerCardList, 250, true);

      start.setEnabled(false);
      again.setEnabled(true);
    }
  }

  class again implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      sendCardToPlayer();
      showCard(playerCardList, 250, true);

      int sum = sumDianShu(computerCardList);
      if (sum < 16) {
        sendCardToComputer();
        showCard(computerCardList, 80, false);
      }
    }
  }

  class end implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      int sum = sumDianShu(computerCardList);
      while (sum < 16) {
        sendCardToComputer();
        sum = sumDianShu(computerCardList);
      }
      showCard(computerCardList, 80, true);
      int sum1 = sumDianShu(playerCardList);
      if (sum > 21 && sum1 > 21) {
      }
      if (sum > 21 && sum1 > 21) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "双方都爆点，再来。。。");
      } else if (sum > 21) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "电脑爆点，玩家赢了");
      } else if (sum1 > 21) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "玩家爆点，电脑赢了");
      } else if (sum1 > sum && sum1 < 22) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "玩家赢了");
      } else if (sum > sum1 && sum < 22) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "电脑赢了");
      } else if (sum == sum1) {
        message.setText("电脑:" + sum + "    "
                        + "玩家:" + sum1 + "    "
                        + "平局");
      }
      start.setText("再来一局");
      start.setEnabled(true);
      again.setEnabled(false);
    }
  }

  class exit implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }
}
